const prefix = 'formio_demo_save';

function set(key: string, value: any) {
  if (!key) return;

  let val = typeof value === 'string' ? value : JSON.stringify(value);
  localStorage.setItem(key, val);
}

function get(name: string) {
  let result: ListData[] = [];
  if (!name) return result;

  const str = localStorage.getItem(name);
  if (!str) return result;

  try {
    result = JSON.parse(str);
  } catch (e) {
    return result;
  }
  return result;
}

function del(name: string) {
  if (!name) return;
  let key: string = prefix;
  localStorage.removeItem(key);
}

export default { get, set, del };
